import { createSlice } from "@reduxjs/toolkit";
import { Toast } from "antd-mobile";

export const TickSlice = createSlice({
  name: "TickSlice",
  initialState: {
    TickListDatas: [
      {
        id: 1,
        name: "甲斗",
        code: "267463716284639824637",
        status: false,
      },
      {
        id: 2,
        name: "蜻蜓",
        code: "736587329874873647348",
        status: false,
      },
      {
        id: 3,
        name: "黄蜂",
        code: "214374283748636647636",
        status: false,
      },
      {
        id: 4,
        name: "钢斗",
        code: "2764372897387834763746",
        status: false,
      },
    ],
  },
  reducers: {

    TickStatus(state,{payload}){
      const item=state.TickListDatas.find((item)=>item.id===payload.id)
      if(item){
        item.state=!item.state
      }
    },

    Tickdel(state,{payload}){
      state.TickListDatas=state.TickListDatas.filter(
        (item)=>item.id!==payload.id
      )
    },
    TickAdd(state,{payload}){
      const item=state.TickListDatas.find((item)=>item.id===payload.id)
      if(!item){
        state.TickListDatas.push(payload)
        Toast.show({
          icon:"success",
          content:"添加成功"
        })
      }else{
        Toast.show({
          icon:"fail",
          content:"已有此人"
        })
      }
    },
    TickEdit(state,{payload}){
      const item=state.TickListDatas.find((item)=>item.id===payload.id)
      if(item){
        Object.assign(item,payload)
        Toast.show({
          icon:"success",
          content:"编辑成功"
        })
      }
    }

  },
});

// Action creators are generated for each case reducer function
export const {TickStatus,TickAdd,TickEdit,Tickdel} = TickSlice.actions;

export default TickSlice.reducer;
